package top.jolyoulu.business.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.jolyoulu.business.web.entity.TestCacheSetFormBO;
import top.jolyoulu.corecommon.cache.LocalCacheOperator;
import top.jolyoulu.corecommon.cache.def.DefaultLocalCacheOperator;
import top.jolyoulu.webcommon.entity.ResultInfo;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author LuZhouJin
 * @Date 2023/4/10
 * core-common-cache 测试
 */
@RestController
@RequestMapping("/test/cache")
public class TestLocalCacheController {

    @Autowired
    private DefaultLocalCacheOperator cacheOperator;

    //测试添加缓存
    @GetMapping("/set")
    public void set(TestCacheSetFormBO bo) {
        cacheOperator.set(bo.getKey(), bo.getValue());
    }

    //测试添加有过期时长的缓存
    @GetMapping("/set/timeOut")
    public void set(@RequestParam String key,
                    @RequestParam String value,
                    @RequestParam Long timeOut) {
        cacheOperator.set(key, value, timeOut, TimeUnit.SECONDS);
    }

    //测试添加获取缓存
    @GetMapping("/get")
    public ResultInfo<String> get(@RequestParam String key) {
        return ResultInfo.success(cacheOperator.get(key).toString());
    }

    //测试获取全部缓存
    @GetMapping("/getAll")
    public ResultInfo<Set<String>> getAll() {
        return ResultInfo.success(cacheOperator.keys());
    }

}
